//
//  OYHomeCell.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/5.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit

class OYHomeCell: UITableViewCell {

    var toolBarContraint: Constraint?
    
    
     // MARK: -懒加载
    lazy var originalView: OYHomeOriginalView = OYHomeOriginalView()
    lazy var retweetView: OYHomeRetweetView = OYHomeRetweetView()
    lazy var toolBarView: OYHomeToolBarView = OYHomeToolBarView()
    
    var homeStatusVM: OYStatusViewModel?{
        didSet {
            originalView.statusVM = homeStatusVM
            toolBarView.statusVM = homeStatusVM
            retweetView.statusVM = homeStatusVM
            
            // 卸载约束
            toolBarContraint?.uninstall()
            if homeStatusVM?.statusModel?.retweeted_status != nil {
                retweetView.isHidden = false
                toolBarView.snp_updateConstraints(closure: { (make) in
                    toolBarContraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else {
                retweetView.isHidden = true
                toolBarView.snp_updateConstraints(closure: { (make) in
                    toolBarContraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        self.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
        
    }
    
    
    
    func setupUI() {
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
                
        originalView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self.contentView)
//            make.height.equalTo(100)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.left.right.equalTo(self.contentView)
            make.top.equalTo(originalView.snp_bottom).offset(5)
            
        }
        
        toolBarView.snp_makeConstraints { (make) in
            make.left.right.equalTo(self.contentView)
            toolBarContraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
             // MARK: -自动计算行高
            make.bottom.equalTo(contentView).offset(-25)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
